package com.feige.aop;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;



/**
 * @author feige<br />
 * @ClassName: AopConfig <br/>
 * @Description: <br/>
 * @date: 2021/6/6 12:22<br/>
 */
@Component
@Aspect
@Slf4j
public class AopConfig {

    @Pointcut(value = "@annotation(com.feige.annotation.Log)")
    public void pointcut(){

    }

    /**
     * @description: 环绕通知
     * @author: feige
     * @date: 2021/6/6 12:36
     * @param	joinPoint
     * @return: void
     */
    @Around(value = "pointcut()")
    public Object around(ProceedingJoinPoint joinPoint){
        try {
            Object[] args = joinPoint.getArgs();
            String clazz = joinPoint.getSignature().getDeclaringTypeName();
            String name = joinPoint.getSignature().getName();
            log.info("执行{}.{}方法，参数为{}",clazz,name, JSONUtil.toJsonStr(args));
            Object proceed = joinPoint.proceed();
            log.info("{}.{}方法执行成功，返回值为{}",clazz,name,JSONUtil.toJsonStr(proceed));
            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }
    /**
     * @description: 抛出异常
     * @author: feige
     * @date: 2021/6/6 13:22
     * @param
     * @return: void
     */
    @AfterThrowing(value = "pointcut()",throwing = "ex")
    public void afterThrowing(Throwable ex){
        log.error("发生异常，异常信息为{}",ex.getMessage());
    }

}
